package com.maddox.maddoxppjoke.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.paging.PagedList;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.maddox.libcommon.view.EmptyView;
import com.maddox.maddoxppjoke.AbsViewModel;
import com.maddox.maddoxppjoke.R;
import com.maddox.maddoxppjoke.databinding.LayoutRefreshViewBinding;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 通用的页面刷新列表
 */
public abstract class AbsListFragment<T, M extends AbsViewModel<T>> extends Fragment implements OnRefreshListener, OnLoadMoreListener {
    LayoutRefreshViewBinding binding;
    private RecyclerView recyclerView;
    private SmartRefreshLayout refreshLayout;
    private EmptyView emptyView;
    private PagedListAdapter<T, RecyclerView.ViewHolder> listAdapter;

    protected M mViewModel;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        genericViewModel();
    }

    private void genericViewModel() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Type[] arguments = type.getActualTypeArguments();
        if (arguments.length > 1) {
            Type argue = arguments[1];
            Class subclass = ((Class) argue).asSubclass(AbsViewModel.class);
            mViewModel = (M) ViewModelProviders.of(this).get(subclass);
            //触发页面数据初始化加载
            mViewModel.getPageData().observe(getViewLifecycleOwner(), pagedList -> submitList(pagedList));
            //监听分页有无数据来决定是否关闭上拉加载动画
            mViewModel.getBoundaryPageData().observe(getViewLifecycleOwner(), hasData -> finishRefresh(hasData));
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        binding = DataBindingUtil.inflate(inflater, R.layout.layout_refresh_view, container, false);
        recyclerView = binding.recyclerView;
        refreshLayout = binding.refreshLayout;
        emptyView = binding.emptyView;
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        //设置适配器
        listAdapter = getAdapter();
        recyclerView.setAdapter(listAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.HORIZONTAL));
        //子类去重写一些定义,暴露一个方法
        afterCreateView();

        return binding.getRoot();
    }

    protected abstract void afterCreateView();

    //根据是否有数据来判断展示空布局
    public void finishRefresh(boolean hasData) {
        PagedList<T> currentList = listAdapter.getCurrentList();
        hasData = hasData || currentList != null && currentList.size() > 0;
        RefreshState state = refreshLayout.getState();
        if (state.isFooter && state.isOpening) {
            refreshLayout.finishLoadMore();
        } else if (state.isHeader && state.isOpening) {
            refreshLayout.finishRefresh();
        }
        if (hasData) {
            emptyView.setVisibility(View.GONE);
        } else {
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    public void submitList(PagedList<T> pagedList) {
        if (pagedList.size() > 0) {
            listAdapter.submitList(pagedList);
        }
        finishRefresh(pagedList.size() > 0);
    }

    public abstract PagedListAdapter<T, RecyclerView.ViewHolder> getAdapter();


}
